# 导包
import easygui
import pygame
import sys
import random
import time
import json

# pygame初始化
pygame.init()
# 创建pygame屏幕
canvas = pygame.display.set_mode((1232, 765))
pygame.display.set_caption('逃生')
# load image
bg = pygame.image.load('escape-master/bg.png')
running_bg = pygame.image.load('escape-master/RUNNING_bg.png')
people = pygame.image.load('escape-master/stand.png')
people_behind = pygame.image.load('escape-master/stand_behind.png')
people_left = pygame.image.load('escape-master/stand_left.png')
people_right = pygame.image.load('escape-master/stand_right.png')
ak_img = pygame.image.load('escape-master/ak.png')
pistol_img = pygame.image.load('escape-master/pistol.png')
knife_img = pygame.image.load('escape-master/forty_knife.png')
ak_tip = pygame.image.load('escape-master/take_ak_tip.png')
box = pygame.image.load('escape-master/thing_box.png')
ak2_img = pygame.image.load('escape-master/ak2.png')
take_ak = pygame.image.load('escape-master/take_ak.png')
take_ak_behind = pygame.image.load('escape-master/take_ak_behind.png')
take_ak_left = pygame.image.load('escape-master/take_ak_left.png')
take_ak_right = pygame.image.load('escape-master/take_ak_right.png')
take_pistol = pygame.image.load('escape-master/take_pistol.png')
take_pistol_behind = pygame.image.load('escape-master/take_pistol_behind.png')
take_knife = pygame.image.load('escape-master/take_knife.png')
take_knife_behind = pygame.image.load('escape-master/take_knife_behind.png')
bullet_img = pygame.image.load('escape-master/bullet.png')
pistol_tip = pygame.image.load('escape-master/take_pistol_tip.png')
knife_cut = pygame.image.load('escape-master/cut.png')
knife_cut_behind = pygame.image.load('escape-master/cut_behind.png')
carpet = pygame.image.load('escape-master/carpet.png')
knife_tip = pygame.image.load('escape-master/take_knife_tip.png')
knife2_img = pygame.image.load('escape-master/forty_knife1.png')
take_knife_left = pygame.image.load('escape-master/take_knife_left.png')
take_knife_right = pygame.image.load('escape-master/take_knife_right.png')
cut_left = pygame.image.load('escape-master/cut_left.png')
cut_right = pygame.image.load('escape-master/cut_right.png')
take_pistol_left = pygame.image.load('escape-master/take_pistol_left.png')
take_pistol_right = pygame.image.load('escape-master/take_pistol_right.png')
door_img = pygame.image.load('escape-master/door.png')
zombie_stand = pygame.image.load('escape-master/zombie.png')
open_door_tip = pygame.image.load('escape-master/open_door_tip.png')
level3_bg = pygame.image.load('escape-master/level3_bg.png')
zombie_behind = pygame.image.load('escape-master/zombie_behind.png')
zombie_attack = pygame.image.load('escape-master/zombie_attack.png')
zombie_attack_behind = pygame.image.load('escape-master/zombie_attack_behind.png')
ammunition_depot_img = pygame.image.load('escape-master/Ammunition depot.png')
load_bullet_tip = pygame.image.load('escape-master/load_bullet_tip.png')
sysBox = pygame.image.load('escape-master/sysBox.png')
bullet_load_icon = pygame.image.load('escape-master/bullet_loading.png')
treatment_box_img = pygame.image.load('escape-master/treatment_box.png')
return_life_icon = pygame.image.load('escape-master/return life.png')
machine_gun1 = pygame.image.load('escape-master/machine_gun.png')
machine_gun2 = pygame.image.load('escape-master/machine_gun2.png')
takeMachine_gun_tip = pygame.image.load('escape-master/takeMachine_gun_tip.png')
take_machineGun_up = pygame.image.load('escape-master/take_machineGun_up.png')
take_machineGun_right = pygame.image.load('escape-master/take_machineGun_right.png')
take_machineGun_left = pygame.image.load('escape-master/take_machineGun_left.png')
take_machineGun_down = pygame.image.load('escape-master/take_machineGun_down.png')
white = pygame.image.load('escape-master/white.png')
pick_up_pistol_score = pygame.image.load('escape-master/pick_up_pistol_score.png')
pick_up_machineGun_score = pygame.image.load('escape-master/pick_up_machineGun_score.png')
pick_up_knife_score = pygame.image.load('escape-master/pick_up_knife_score.png')
hit_wall10_score = pygame.image.load('escape-master/hit_wall_score.png')
hit_zombies50_score = pygame.image.load('escape-master/hit_zombies_50.png')
hit_zombies_life10_score = pygame.image.load('escape-master/hit_zombies_life10score.png')
hit_zombies_strong_score = pygame.image.load('escape-master/hit_zombies_strong_score.png')
return_life_score = pygame.image.load('escape-master/return_life_score.png')
return_bullet_score = pygame.image.load('escape-master/return_bullet_score.png')
return_life_score_not = pygame.image.load('escape-master/return_life_score_not.png')
return_bullet_score_not = pygame.image.load('escape-master/return_bullet_score_not.png')
pick_up_pistol_score_not = pygame.image.load('escape-master/pick_up_pistol_score_not.png')
pick_up_machineGun_score_not = pygame.image.load('escape-master/pick_up_machineGun_score_not.png')
pick_up_knife_score_not = pygame.image.load('escape-master/pick_up_knife_score_not.png')
hit_wall10_score_not = pygame.image.load('escape-master/hit_wall10_score_not.png')
hit_zombies50_score_not = pygame.image.load('escape-master/hit_zombies_50_score_not.png')
hit_zombies_life10_score_not = pygame.image.load('escape-master/hit_zombies_life10score_not.png')
hit_zombies_strong_score_not = pygame.image.load('escape-master/hit_zombies_strong_score_not.png')
achievement_column = pygame.image.load('escape-master/Achievement_column.png')
game_over_img = pygame.image.load('escape-master/GameOver_img.png')
ranking_list = pygame.image.load('escape-master/ranking list.png')
operation_guide = pygame.image.load('escape-master/operation guide.png')
# 成就列表编号
achievement_pip = [9, 9, 9, 9, 9, 9, 9, 9, 9]
# 时间
time_now = time.strftime("%Y-%m-%d", time.localtime())
# 用户名称
username = ''
# 用户密码
password = ''
# 定义状态
state = 'START'
# 定义回复信息储存
answer = 0
# 按键列表
keys = {'UP': False, 'DOWN': False, 'LEFT': False, 'RIGHT': False, 'PistolE': True, 'KnifeE': True, 'AKE': True,
        'MachineGun': True}
# 定义画出拾取ak的提示
ak_tipPaint = False
# 定义画出ak的开关
ak_switch = True
# 储存物品的列表
things = []
# 定义方向
direction = 'UP'
# 手上物资
material = 0
# 子弹列表
bullets = []
# 子弹优先值
first = 0
# 画出提示拾取手枪
pistol_tipPaint = 0
# 画出手枪
pistol_switch = True
# 各个东西的变量
thing_1 = 0
thing_2 = 0
thing_3 = 0
# 画出提示拾取刀
knife_tipPaint = False
# 画出刀
knife_switch = True
# 定义关卡变量
level = 1
# 画出提示开门的开关
door_tipPaint = False
# 僵尸列表
zombies = []
# 僵尸数量
zombie_num = 20
zombie_num_init = 20
# 僵尸的图片
zombie_img = zombie_stand
# 僵尸的方向
direction_zombie = 'DOWN'
# 僵尸碰到玩家换图片开关
zombie_hit = False
# 技能的开始时间
cut_currentTime = 0
# 技能的结束时间
cut_lastTime = 0
# 技能的需要经过的时间
cut_interval = 0
# 旁白开关
sys_switch = 0
# 画出提示加子弹开关
plus_bullet_tip = False
# ak加子弹的开始时间
ak_bullet_currentTime = 0
# ak加子弹的经过时间
ak_bullet_interval = 0
# ak加子弹的结束时间
ak_bullet_lastTime = 0
# ak加子弹开关
ak_bullet_loadSwitch = False
# 手枪加子弹的开始时间
pistol_bullet_currentTime = 0
# 手枪加子弹的经过时间
pistol_bullet_interval = 0
# 手枪加子弹的结束时间
pistol_bullet_lastTime = 0
# 手枪加子弹开关
pistol_bullet_loadSwitch = False
# 波数
wave = 1
# 画出回血的开关
return_life_tip = False
# 回血的开始时间
returnLife_currentTime = 0
# 回血的经过时间
returnLife_interval = 0
# 回血的结束时间
returnLife_lastTime = 0
# 加血开关
returnLife_switch = False
# 画出提示拾取机枪的开关
paint_takeMachineGun = False
# 画出机枪的开关
paint_MachineGun = True
# 机枪加子弹的开始时间
machine_gun_bullet_currentTime = 0
# 机枪加子弹的经过时间
machine_gun_bullet_interval = 0
# 机枪加子弹的结束时间
machine_gun_bullet_lastTime = 0
# 机枪加子弹的开关
machine_gun_bullet_loadSwitch = False
# 成就列表的坐标
achievement_pos = [[279, 202], [508, 202], [737, 202], [279, 333], [508, 333], [737, 333], [279, 463], [508, 463],
                   [737, 463]]
# 排行榜的y坐标
ranking_list_y = [150, 225, 300, 375, 450, 525, 600]
# 游戏结束开关
gameOver = False
# 击杀僵尸的数量
kill_num = 0
# 撞墙次数
hit_wall = 0
# 登录注册游客状态
action = ''

# 定义写字方法
def fillText(count, position, colour):
    TextFont = pygame.font.Font('escape-master/font.ttf', 20)
    NewFont = TextFont.render(count, True, colour)
    canvas.blit(NewFont, position)


# 定义旁白
def sysFont(count):
    canvas.blit(sysBox, (44, 563))
    fillText(count, (68, 580), (0, 0, 0))


# 定义初始游戏的方法
def initGame():
    global state, answer, keys, ak_tipPaint, ak_switch, things, direction, material, bullets, first, pistol_tipPaint, pistol_switch, thing_1, thing_2, thing_3
    global knife_switch, knife_tipPaint, level, door_tipPaint, zombies, zombie_num, zombie_num_init, zombie_img, direction_zombie, zombie_hit
    global cut_currentTime, cut_lastTime, cut_interval, sys_switch, plus_bullet_tip, ak_bullet_currentTime, ak_bullet_lastTime, ak_bullet_interval
    global ak_bullet_loadSwitch, pistol_bullet_loadSwitch, pistol_bullet_currentTime, pistol_bullet_lastTime, pistol_bullet_interval, wave, return_life_tip
    global returnLife_currentTime, returnLife_interval, returnLife_lastTime, returnLife_switch, paint_MachineGun, paint_takeMachineGun, machine_gun_bullet_currentTime, machine_gun_bullet_interval, machine_gun_bullet_lastTime
    global machine_gun_bullet_loadSwitch, kill_num, hit_wall, player, pistol, knife, door, ammunition_depot, treatment_box, machine_gun, gameOver
    # pygame初始化
    pygame.init()
    # 定义状态
    state = 'START'
    # 定义回复信息储存
    answer = 0
    # 按键列表
    keys = {'UP': False, 'DOWN': False, 'LEFT': False, 'RIGHT': False, 'PistolE': True, 'KnifeE': True, 'AKE': True,
            'MachineGun': True}
    # 定义画出拾取ak的提示
    ak_tipPaint = False
    # 定义画出ak的开关
    ak_switch = True
    # 储存物品的列表
    things = []
    # 定义方向
    direction = 'UP'
    # 手上物资
    material = 0
    # 子弹列表
    bullets = []
    # 子弹优先值
    first = 0
    # 画出提示拾取手枪
    pistol_tipPaint = 0
    # 画出手枪
    pistol_switch = True
    # 各个东西的变量
    thing_1 = 0
    thing_2 = 0
    thing_3 = 0
    # 画出提示拾取刀
    knife_tipPaint = False
    # 画出刀
    knife_switch = True
    # 定义关卡变量
    level = 1
    # 画出提示开门的开关
    door_tipPaint = False
    # 僵尸列表
    zombies = []
    # 僵尸数量
    zombie_num = 20
    zombie_num_init = 20
    # 僵尸的图片
    zombie_img = zombie_stand
    # 僵尸的方向
    direction_zombie = 'DOWN'
    # 僵尸碰到玩家换图片开关
    zombie_hit = False
    # 技能的开始时间
    cut_currentTime = 0
    # 技能的结束时间
    cut_lastTime = 0
    # 技能的需要经过的时间
    cut_interval = 0
    # 旁白开关
    sys_switch = 0
    # 画出提示加子弹开关
    plus_bullet_tip = False
    # ak加子弹的开始时间
    ak_bullet_currentTime = 0
    # ak加子弹的经过时间
    ak_bullet_interval = 0
    # ak加子弹的结束时间
    ak_bullet_lastTime = 0
    # ak加子弹开关
    ak_bullet_loadSwitch = False
    # 手枪加子弹的开始时间
    pistol_bullet_currentTime = 0
    # 手枪加子弹的经过时间
    pistol_bullet_interval = 0
    # 手枪加子弹的结束时间
    pistol_bullet_lastTime = 0
    # 手枪加子弹开关
    pistol_bullet_loadSwitch = False
    # 波数
    wave = 1
    # 画出回血的开关
    return_life_tip = False
    # 回血的开始时间
    returnLife_currentTime = 0
    # 回血的经过时间
    returnLife_interval = 0
    # 回血的结束时间
    returnLife_lastTime = 0
    # 加血开关
    returnLife_switch = False
    # 画出提示拾取机枪的开关
    paint_takeMachineGun = False
    # 画出机枪的开关
    paint_MachineGun = True
    # 机枪加子弹的开始时间
    machine_gun_bullet_currentTime = 0
    # 机枪加子弹的经过时间
    machine_gun_bullet_interval = 0
    # 机枪加子弹的结束时间
    machine_gun_bullet_lastTime = 0
    # 机枪加子弹的开关
    machine_gun_bullet_loadSwitch = False
    # 撞墙次数
    hit_wall = 0
    player = Player(481, 550, people)
    pistol = Pistol(random.randint(195, 1090 - 50), 495, pistol_img)
    knife = Knife(random.randint(194, 1089 - 54), random.randint(228, 708 - 193), knife_img)
    door = Door(600, 100, door_img)
    ammunition_depot = Ammunition_depot(987, 500, ammunition_depot_img)
    treatment_box = Treatment_box(987, 200, treatment_box_img)
    machine_gun = Machine_gun(random.randint(194, 1089 - 54), random.randint(228, 708 - 193), machine_gun1)
    # 游戏结束开关
    gameOver = False


# 登录or注册or游客
var = True
while True:
    action = easygui.choicebox('登录or注册or游客', '询问框', ['登录', '注册', '游客'])
    if action == '登录' or action == '注册' or action == '游客':
        break
    elif action is None:
        easygui.msgbox('请选择方式进入游戏')
if action == '注册':
    while True:
        user = easygui.multpasswordbox('请设置你的账号(1-10位)和密码(5-20)', '注册', ['用户名:', '密码:'])
        if user == []:
            easygui.msgbox('账号密码为空', '提示')
        elif user is None:
            easygui.msgbox('账号密码为空', '提示')
        elif len(user[0]) < 1 or len(user[0]) > 10:
            easygui.msgbox('账号长度错误')
        elif len(user[1]) < 5 or len(user[1]) > 20:
            easygui.msgbox('密码长度错误')
        elif password.isalnum():
            easygui.msgbox('密码需要为数字字母组合')
        elif username.isalpha():
            easygui.msgbox('账号需要为字母')
        else:
            username = user[0]
            password = user[1]
            with open('escape-master/user_register.txt', 'r', encoding='utf-8') as f:
                user_info = f.readlines()
            if user_info == []:
                info = {'name': username, 'password': password}
                info = json.dumps(info)
                with open('escape-master/user_register.txt', 'a', encoding='utf-8') as f:
                    f.write(info + '\n')
                    easygui.msgbox('注册完成,保存成功')
                new_info = {'name': username, 'kill': -1, 'wave': 1, 'achievement': [9,9,9,9,9,9,9,9,9], 'time': time_now}
                new_info = json.dumps(new_info)
                with open('escape-master/info.txt', 'a', encoding='utf') as f:
                    f.write(new_info + '\n')
                    break
            else:
                for c in user_info:
                    c = c.strip()
                    user_data = json.loads(c)
                    user_dataName = user_data['name']
                if user_dataName == username:
                    easygui.msgbox('用户名重复')
                else:
                    info = {'name': username, 'password': password}
                    info = json.dumps(info)
                    with open('escape-master/user_register.txt', 'a', encoding='utf-8') as f:
                        f.write(info + '\n')
                        easygui.msgbox('注册完成,保存成功')
                    new_info = {'name': username, 'kill': -1, 'wave': 1, 'achievement': [9, 9, 9, 9, 9, 9, 9, 9, 9],
                                'time': time_now}
                    new_info = json.dumps(new_info)
                    with open('escape-master/info.txt', 'a', encoding='utf') as f:
                        f.write(new_info + '\n')
                        break

elif action == '登录':
    while var:
        user = easygui.multpasswordbox('请填写账号密码', '登录', ['用户名:', '密码:'])
        if user is None:
            easygui.msgbox('你没有填写任何的东西')
        elif user == []:
            easygui.msgbox('你没有输入任何东西')
        else:
            with open('escape-master/user_register.txt', 'r', encoding='utf-8') as f:
                user_info = f.readlines()
            if user_info == []:
                answer = easygui.buttonbox('您没有在此游戏的登录信息记录,请重启游戏注册或者使用游客模式', '询问', ['退出重新注册', '游客模式'])
                if answer == '游客':
                    action = '游客'
                    break
                else:
                    easygui.msgbox('正在退出游戏')
                    pygame.time.delay(5)
                    sys.exit()

            for c in user_info:
                c = c.strip()
                user_data = json.loads(c)
                user_name = user_data['name']
                user_password = user_data['password']
                if user_name == user[0] and user_password == user[1]:
                    easygui.msgbox(f'登录成功,欢迎{user[0]}')
                    username = user[0]
                    password = user[1]
                    var = False
                    break
            if var:
                easygui.msgbox('账号或密码错误')
elif action == '游客':
    t = str(time.time())
    stime = t[-1:-5:-1]
    username = '游客' + str(stime)
    password = 0
    info = {'name': username, 'password': password}
    info = json.dumps(info)
    with open('escape-master/user_register.txt', 'a', encoding='utf-8') as f:
        f.write(info + '\n')
        easygui.msgbox('您的游戏状态为游客模式')
    new_info = {'name': username, 'kill': -1, 'wave': 1, 'achievement': [9,9,9,9,9,9,9,9,9], 'time': time_now}
    new_info = json.dumps(new_info)
    with open('escape-master/info.txt', 'a', encoding='utf') as f:
        f.write(new_info + '\n')

with open('escape-master/info.txt', 'r', encoding='utf-8') as f:
    data = f.readlines()
for c in data:
    c = c.strip()
    newdata = json.loads(c)
    if newdata['name'] == username:
        if 0 in newdata['achievement']:
            achievement_pip[0] = 0
        if 1 in newdata['achievement']:
            achievement_pip[1] = 1
        if 2 in newdata['achievement']:
            achievement_pip[2] = 2
        if 3 in newdata['achievement']:
            achievement_pip[3] = 3
        if 4 in newdata['achievement']:
            achievement_pip[4] = 4
        if 5 in newdata['achievement']:
            achievement_pip[5] = 5
        if 6 in newdata['achievement']:
            achievement_pip[6] = 6
        if 7 in newdata['achievement']:
            achievement_pip[7] = 7
        if 8 in newdata['achievement']:
            achievement_pip[8] = 8

# 创建存档方法
def file():
    info = {'name': username, 'kill': kill_num, 'wave': wave, 'achievement': achievement_pip, 'time': time_now}
    with open('escape-master/info.txt', 'r') as f:
        data = f.readlines()
    data_list = []
    for i in data:
        d = i.strip()
        data_list.append(json.loads(d))
    if len(data) == 0:
        info = json.dumps(info)
        with open('escape-master/info.txt', 'w', encoding='utf-8') as f:
            f.write(info + '\n')
    else:
        if len(data_list) < 7:
            m = 1
            for num in range(len(data_list) + 1):
                if info['kill'] >= data_list[num]['kill'] and m == 1:
                    data_list.insert(num, info)
                    m = 2
                if data_list[num]['kill'] == -1:
                    data_list.pop()
        else:
            for num in range(len(data_list)):
                if info['kill'] >= data_list[num]['kill']:
                    data_list.insert(num, info)
                    data_list.pop()
                    break
        with open('escape-master/info.txt', 'w') as f:
            for new_data in data_list:
                new_json_data = json.dumps(new_data)
                f.write(new_json_data + '\n')


# 创建玩家类
class Player(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 0
        self.height = 0
        self.life = 100

    def paint(self):
        global material
        if material == 0:
            if direction == 'UP':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y - 30), (player.life, 10)))
                self.img = people
            elif direction == 'DOWN':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + 20, player.y + player.height + 20), (player.life, 10)))
                self.img = people_behind
            elif direction == 'LEFT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x - 20, player.y + 20), (10, player.life)))
                self.img = people_left
            elif direction == 'RIGHT':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + player.width + 20, player.y + 20), (10, player.life)))
                self.img = people_right
            canvas.blit(self.img, (self.x, self.y))
        elif material == 'ak':
            if direction == 'UP':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y - 30), (player.life, 10)))
                self.img = take_ak
                canvas.blit(self.img, (self.x, self.y - 89))
            elif direction == 'DOWN':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + 20, player.y + player.height - 80), (player.life, 10)))
                self.img = take_ak_behind
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'LEFT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x - 30, player.y + 20), (10, player.life)))
                self.img = take_ak_left
                canvas.blit(self.img, (self.x - 87, self.y))
            elif direction == 'RIGHT':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + player.width - 80, player.y + 20), (10, player.life)))
                self.img = take_ak_right
                canvas.blit(self.img, (self.x, self.y))
        elif material == 'knife':
            if direction == 'UP':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y - 20), (player.life, 10)))
                self.img = take_knife
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'DOWN':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y + 100), (player.life, 10)))
                self.img = take_knife_behind
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'LEFT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x, player.y + 20), (10, player.life)))
                self.img = take_knife_left
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'RIGHT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 100, player.y + 20), (10, player.life)))
                self.img = take_knife_right
                canvas.blit(self.img, (self.x, self.y))
        elif material == 'cut':
            if direction == 'UP':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y - 50), (player.life, 10)))
                self.img = knife_cut
                canvas.blit(self.img, (self.x, self.y - 143))
            elif direction == 'DOWN':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y + 100), (player.life, 10)))
                self.img = knife_cut_behind
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'LEFT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x, player.y + 20), (10, player.life)))
                self.img = cut_left
                canvas.blit(self.img, (self.x - 143, self.y))
            else:
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 100, player.y + 20), (10, player.life)))
                self.img = cut_right
                canvas.blit(self.img, (self.x, self.y))
        elif material == 'pistol':
            if direction == 'UP':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y - 20), (player.life, 10)))
                self.img = take_pistol
            elif direction == 'DOWN':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + 20, player.y + player.height + 20), (player.life, 10)))
                self.img = take_pistol_behind
            elif direction == 'LEFT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x - 20, player.y + 20), (10, player.life)))
                self.img = take_pistol_left
            else:
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + player.width + 20, player.y + 20), (10, player.life)))
                self.img = take_pistol_right
            canvas.blit(self.img, (self.x, self.y))
        elif material == 'machine_gun':
            if direction == 'UP':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x + 20, player.y + 10), (player.life, 10)))
                self.img = take_machineGun_up
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'DOWN':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + 20, player.y + player.height + 20), (player.life, 10)))
                self.img = take_machineGun_down
                canvas.blit(self.img, (self.x, self.y))
            elif direction == 'LEFT':
                pygame.draw.rect(canvas, (0, 255, 0), ((player.x - 50, player.y + 20), (10, player.life)))
                self.img = take_machineGun_left
                canvas.blit(self.img, (self.x - 87, self.y))
            elif direction == 'RIGHT':
                pygame.draw.rect(canvas, (0, 255, 0),
                                 ((player.x + player.width, player.y + 20), (10, player.life)))
                self.img = take_machineGun_right
                canvas.blit(self.img, (self.x, self.y))

    def hit(self, c):
        return c.x + c.width >= self.x and c.y + c.height >= self.y and self.x + self.width >= c.x and self.y + self.height >= c.y


player = Player(481, 550, people)


# 创建AK类
class AK(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 100
        self.height = 100
        self.bullet_num = 50

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))


ak = AK(random.randint(195, 1090 - 100), 311, ak_img)


# 创建子弹类
class Bullet(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 10
        self.height = 10
        self.speed = 50

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))

    def move(self):
        self.y += - self.speed

    def hit(self, c):
        return c.x + c.width >= self.x and c.y + c.height >= self.y and self.x + self.width >= c.x and self.y + self.height >= c.y


# 创建手枪类
class Pistol(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 50
        self.height = 36
        self.bullet_num = 7

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))

    def hit(self, c):
        return c.x + c.width >= self.x and c.y + c.height >= self.y and self.x + self.width >= c.x and self.y + self.height >= c.y


pistol = Pistol(random.randint(195, 1090 - 50), 495, pistol_img)


# 创建刀类
class Knife(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 54
        self.height = 193

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))


knife = Knife(random.randint(194, 1089 - 54), random.randint(228, 708 - 193), knife_img)


# 创建门类
class Door(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 200
        self.height = 200

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))


door = Door(600, 100, door_img)


# 创建僵尸类
class Zombie(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 0
        self.height = 0
        self.speed = 1
        self.life = 50
        if wave == 3:
            self.life = 80
        elif wave == 4:
            self.life = 100
        self.fx = 0
        self.fy = 0

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))

    def move(self, speed):
        pygame.draw.rect(canvas, (255, 0, 0), ((self.x, self.y), (self.life, 5)))
        global direction_zombie
        self.fx = (player.x - self.x) // 10
        self.fy = (player.y - self.y) // 10
        self.x = self.x + self.fx * (speed // 10)
        self.y = self.y + self.fy * (speed // 10)
        if player.y < self.y + self.height:
            self.img = zombie_img
            direction_zombie = 'DOWN'
        if self.y > player.y + player.height:
            self.img = zombie_behind
            direction_zombie = 'UP'
        if zombie_hit:
            if direction_zombie == 'UP':
                self.width = 95
                self.height = 99
                self.img = zombie_attack_behind
                canvas.blit(self.img, (self.x, self.y))
                pygame.draw.rect(canvas, (255, 0, 0), ((self.x, self.y - 10), (self.life, 5)))
            elif direction_zombie == 'DOWN':
                self.width = 97
                self.height = 101
                self.img = zombie_attack
                canvas.blit(self.img, (self.x, self.y))
                pygame.draw.rect(canvas, (255, 0, 0), ((self.x, self.y), (self.life, 5)))
        else:
            if direction_zombie == 'UP':
                self.width = 103
                self.height = 83
                self.img = zombie_behind
                canvas.blit(self.img, (self.x, self.y))
            else:
                self.width = 104
                self.height = 85
                self.img = zombie_img
                canvas.blit(self.img, (self.x, self.y))

    def hit(self, c):
        return c.x + c.width >= self.x and c.y + c.height >= self.y and self.x + self.width >= c.x and self.y + self.height >= c.y


# 创建弹药库类
class Ammunition_depot(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 218
        self.height = 262

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))

    def hit(self, c):
        return c.x + c.width >= self.x and c.y + c.height >= self.y and self.x + self.width >= c.x and self.y + self.height >= c.y


ammunition_depot = Ammunition_depot(987, 500, ammunition_depot_img)


# 创建治疗箱类
class Treatment_box(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 217
        self.height = 263

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))

    def hit(self, c):
        return c.x + c.width >= self.x and c.y + c.height >= self.y and self.x + self.width >= c.x and self.y + self.height >= c.y


treatment_box = Treatment_box(987, 200, treatment_box_img)


# 创建机枪类
class Machine_gun(object):
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = 100
        self.height = 100
        self.bullet_num = 200

    def paint(self):
        canvas.blit(self.img, (self.x, self.y))


machine_gun = Machine_gun(random.randint(194, 1089 - 54), random.randint(228, 708 - 193), machine_gun1)


# 创建handleEvent方法
def handleEvent():
    global answer, state, ak_tipPaint, ak_switch, direction, material, first, pistol_tipPaint, pistol_switch, thing_1, thing_2, x, y, ak_bullet_interval, ak_bullet_loadSwitch, keys, pistol_bullet_currentTime, returnLife_switch, things, knife_tipPaint, knife_switch, thing_3, level, cut_currentTime, cut_interval, sys_switch, ak_bullet_currentTime, pistol_bullet_interval, pistol_bullet_loadSwitch, returnLife_currentTime, returnLife_interval, paint_MachineGun, paint_takeMachineGun, machine_gun_bullet_currentTime, machine_gun_bullet_loadSwitch, machine_gun_bullet_interval, wave
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            x = event.pos[0]
            y = event.pos[1]
            if state == 'START':
                if level == 1:
                    if 858 <= x <= 1090 and 384 <= y <= 453:
                        answer = easygui.buttonbox('是否开始游戏', '询问', ['是的', '不'])
                        if answer == '是的':
                            state = 'RUNNING'
                            level = 2
                            sys_switch = 'sport'
                    if 859 <= x <= 1089 and 473 <= y <= 542:
                        level = 'open Achievement column'
                    if 859 <= x <= 1089 and 650 <= y <= 726:
                        level = 'open ranking list'
                    if 859 <= x <= 1089 and 565 <= y <= 636:
                        level = 'open operation guide'
                if level == 'open Achievement column':
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        if 948 <= x <= 1014 and 117 <= y <= 160:
                            level = 1
                if level == 'open ranking list':
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        if 851 <= x <= 923 and 36 <= y <= 91:
                            level = 1
                if level == 'open operation guide':
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        if 966 <= x <= 1067 and 20 <= y <= 52:
                            level = 1
        if sys_switch != 'loading bullet' and sys_switch != 'return life':
            if state == 'RUNNING':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_a:
                        keys['LEFT'] = True
                        direction = 'LEFT'
                    elif event.key == pygame.K_d:
                        keys['RIGHT'] = True
                        direction = 'RIGHT'
                    elif event.key == pygame.K_w:
                        keys['UP'] = True
                        direction = 'UP'
                    elif event.key == pygame.K_s:
                        keys['DOWN'] = True
                        direction = 'DOWN'

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_a:
                        keys['LEFT'] = False
                    elif event.key == pygame.K_d:
                        keys['RIGHT'] = False
                    elif event.key == pygame.K_w:
                        keys['UP'] = False
                    elif event.key == pygame.K_s:
                        keys['DOWN'] = False
            if level != 3:
                if ak_tipPaint:
                    if keys['AKE']:
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_e:
                                if len(things) < 3:
                                    ak_tipPaint = False
                                    ak_switch = False
                                    if len(things) == 0:
                                        things.append(AK(5, 154, ak2_img))
                                        thing_1 = 'ak'
                                        keys['AKE'] = False
                                    elif len(things) == 1:
                                        things.append(AK(5, 229, ak2_img))
                                        thing_2 = 'ak'
                                        keys['AKE'] = False
                                    elif len(things) == 2:
                                        things.append(AK(5, 305, ak2_img))
                                        thing_3 = 'ak'
                                        keys['AKE'] = False
                                    material = 'ak'
                                else:
                                    sys_switch = 'things_full'

            if material == 'ak':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_j:
                        sys_switch = 0
                        if ak.bullet_num > 0:
                            if direction == 'UP':
                                bullets.append(Bullet(player.x + player.width - 25, player.y - 100, bullet_img))
                                first = 'UP'
                                ak.bullet_num -= 1
                            if direction == 'DOWN':
                                bullets.append(Bullet(player.x + 16, player.y + player.height + 10, bullet_img))
                                first = 'DOWN'
                                ak.bullet_num -= 1

                            if direction == 'LEFT':
                                bullets.append(Bullet(player.x - 100, player.y + 10, bullet_img))
                                first = 'LEFT'
                                ak.bullet_num -= 1

                            if direction == 'RIGHT':
                                bullets.append(
                                    Bullet(player.x + player.width, player.y + player.height - 18, bullet_img))
                                first = 'RIGHT'
                                ak.bullet_num -= 1

            if material == 'machine_gun':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_j:
                        sys_switch = 0
                        if machine_gun.bullet_num > 0:
                            if direction == 'UP':
                                bullets.append(Bullet(player.x + player.width - 25, player.y - 10, bullet_img))
                                first = 'UP'
                                machine_gun.bullet_num -= 1
                            if direction == 'DOWN':
                                bullets.append(Bullet(player.x + 16, player.y + player.height + 10, bullet_img))
                                first = 'DOWN'
                                machine_gun.bullet_num -= 1

                            if direction == 'LEFT':
                                bullets.append(Bullet(player.x - 100, player.y + 10, bullet_img))
                                first = 'LEFT'
                                machine_gun.bullet_num -= 1

                            if direction == 'RIGHT':
                                bullets.append(
                                    Bullet(player.x + player.width, player.y + player.height - 18, bullet_img))
                                first = 'RIGHT'
                                machine_gun.bullet_num -= 1

            if level != 3:
                if pistol_tipPaint:
                    if keys['PistolE']:
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_e:
                                achievement_pip[0] = 0
                                if len(things) < 3:
                                    sys_switch = 0
                                    pistol_tipPaint = False
                                    pistol_switch = False
                                    if len(things) == 0:
                                        things.append(Pistol(5, 154, pistol_img))
                                        thing_1 = 'pistol'
                                        keys['PistolE'] = False
                                    elif len(things) == 1:
                                        things.append(Pistol(5, 229, pistol_img))
                                        thing_2 = 'pistol'
                                        keys['PistolE'] = False
                                    elif len(things) == 2:
                                        things.append(Pistol(5, 305, pistol_img))
                                        thing_3 = 'pistol'
                                        keys['PistolE'] = False
                                    material = 'pistol'
                                else:
                                    sys_switch = 'things_full'

            if material == 'pistol':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_j:
                        if pistol.bullet_num > 0:
                            if direction == 'UP':
                                bullets.append(Bullet(player.x + player.width - 13, player.y - 10, bullet_img))
                                first = 'UP'
                                pistol.bullet_num -= 1

                            if direction == 'DOWN':
                                bullets.append(Bullet(player.x, player.y + player.height + 15, bullet_img))
                                first = 'DOWN'
                                pistol.bullet_num -= 1

                            if direction == 'LEFT':
                                bullets.append(Bullet(player.x, player.y + 3, bullet_img))
                                first = 'LEFT'
                                pistol.bullet_num -= 1

                            if direction == 'RIGHT':
                                bullets.append(
                                    Bullet(player.x + player.width - 50, player.y + player.height - 15, bullet_img))
                                first = 'RIGHT'
                                pistol.bullet_num -= 1

            if sys_switch != 'loading bullet' and sys_switch != 'return life':
                if len(things) >= 1:
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_1:
                            material = thing_1
                        if event.key == pygame.K_2:
                            material = thing_2
                        if event.key == pygame.K_3:
                            material = thing_3
                        if event.key == pygame.K_q:
                            if material == thing_1:
                                material = thing_2
                            elif material == thing_2:
                                material = thing_3
                            elif material == thing_3:
                                material = thing_1

            if direction == 'UP':
                if material == 'ak':
                    player.width = 148
                    player.height = 162
                elif material == 'pistol':
                    player.width = 152
                    player.height = 68
                elif material == 'knife':
                    player.width = 152
                    player.height = 90
                elif material == 'cut':
                    player.width = 151
                    player.height = 233
                elif material == 0:
                    player.width = 148
                    player.height = 53
                elif material == 'machine_gun':
                    player.width = 149
                    player.height = 119

            elif direction == 'DOWN':
                if material == 'ak':
                    player.width = 149
                    player.height = 162
                elif material == 'pistol':
                    player.width = 150
                    player.height = 66
                elif material == 'knife':
                    player.width = 154
                    player.height = 88
                elif material == 'cut':
                    player.width = 146
                    player.height = 232
                elif material == 0:
                    player.width = 148
                    player.height = 53
                elif material == 'machine_gun':
                    player.width = 150
                    player.height = 117

            elif direction == 'LEFT':
                if material == 'ak':
                    player.width = 162
                    player.height = 149
                if material == 'knife':
                    player.width = 89
                    player.height = 151
                if material == 'cut':
                    player.width = 232
                    player.height = 147
                if material == 'pistol':
                    player.width = 66
                    player.height = 150
                if material == 0:
                    player.width = 56
                    player.height = 147
                elif material == 'machine_gun':
                    player.width = 119
                    player.height = 149

            elif direction == 'RIGHT':
                if material == 'ak':
                    player.width = 163
                    player.height = 148
                if material == 'knife':
                    player.width = 87
                    player.height = 149
                if material == 'cut':
                    player.width = 232
                    player.height = 148
                if material == 'pistol':
                    player.width = 66
                    player.height = 150
                if material == 0:
                    player.width = 54
                    player.height = 148
                elif material == 'machine_gun':
                    player.width = 117
                    player.height = 150

            if len(things) >= 1:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if 5 <= x <= 75 and 153 <= y <= 223:
                        if len(things) == 2:
                            things.clear()
                            if thing_2 == 'ak':
                                things.append(AK(5, 154, ak2_img))
                            if thing_2 == 'pistol':
                                things.append(Pistol(5, 154, pistol_img))
                            if thing_2 == 'knife':
                                things.append(Knife(5, 154, knife2_img))
                            if thing_2 == 'machine_gun':
                                things.append(Machine_gun(5, 154, machine_gun2))
                            thing_1 = thing_2
                            material = thing_1
                        elif len(things) == 3:
                            things.clear()

                            if thing_2 == 'ak':
                                things.append(AK(5, 154, ak2_img))
                            if thing_2 == 'pistol':
                                things.append(Pistol(5, 154, pistol_img))
                            if thing_2 == 'knife':
                                things.append(Knife(5, 154, knife2_img))
                            if thing_2 == 'machine_gun':
                                things.append(Machine_gun(5, 154, machine_gun2))
                            if thing_3 == 'ak':
                                things.append(AK(5, 229, ak2_img))
                            if thing_3 == 'pistol':
                                things.append(Pistol(5, 229, pistol_img))
                            if thing_3 == 'knife':
                                things.append(Knife(5, 229, knife2_img))
                            if thing_3 == 'machine_gun':
                                things.append(Machine_gun(5, 229, machine_gun2))
                            thing_1 = thing_2
                            thing_2 = thing_3
                            material = thing_1
                        elif len(things) == 1:
                            things.clear()
                            thing_1 = 0
                            thing_2 = 0
                            thing_3 = 0
                            material = 0

            if len(things) >= 2:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if 5 <= x <= 75 and 229 <= y <= 299:
                        if len(things) == 2:
                            things.clear()
                            if thing_1 == 'ak':
                                things.append(AK(5, 154, ak2_img))
                            if thing_1 == 'pistol':
                                things.append(Pistol(5, 154, pistol_img))
                            if thing_1 == 'knife':
                                things.append(Knife(5, 154, knife2_img))
                            if thing_1 == 'machine_gun':
                                things.append(Machine_gun(5, 154, machine_gun2))
                        elif len(things) == 3:
                            things.clear()
                            if thing_1 == 'ak':
                                things.append(AK(5, 154, ak2_img))
                            if thing_1 == 'pistol':
                                things.append(Pistol(5, 154, pistol_img))
                            if thing_1 == 'knife':
                                things.append(Knife(5, 154, knife2_img))
                            if thing_1 == 'machine_gun':
                                things.append(Machine_gun(5, 154, machine_gun2))
                            if thing_3 == 'ak':
                                things.append(AK(5, 229, ak2_img))
                            if thing_3 == 'pistol':
                                things.append(Pistol(5, 229, pistol_img))
                            if thing_3 == 'knife':
                                things.append(Knife(5, 229, knife2_img))
                            if thing_3 == 'machine_gun':
                                things.append(Machine_gun(5, 229, machine_gun2))
                            thing_2 = thing_3

            if len(things) == 3:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if 5 <= x <= 75 and 305 <= y <= 375:
                        things.clear()
                        if thing_1 == 'ak':
                            things.append(AK(5, 154, ak2_img))
                        if thing_1 == 'pistol':
                            things.append(Pistol(5, 154, pistol_img))
                        if thing_1 == 'knife':
                            things.append(Knife(5, 154, knife2_img))
                        if thing_1 == 'machine_gun':
                            things.append(Machine_gun(5, 154, machine_gun2))
                        if thing_2 == 'ak':
                            things.append(AK(5, 229, ak2_img))
                        if thing_2 == 'pistol':
                            things.append(Pistol(5, 229, pistol_img))
                        if thing_2 == 'knife':
                            things.append(Knife(5, 229, knife2_img))
                        if thing_2 == 'machine_gun':
                            things.append(Machine_gun(5, 229, machine_gun2))

            if level != 3:
                if knife_tipPaint:
                    if keys['KnifeE']:
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_e:
                                achievement_pip[2] = 2
                                if len(things) < 3:
                                    knife_tipPaint = False
                                    knife_switch = False
                                    if len(things) == 0:
                                        things.append(Knife(5, 154, knife2_img))
                                        thing_1 = 'knife'
                                        keys['KnifeE'] = False
                                    elif len(things) == 1:
                                        things.append(Knife(5, 229, knife2_img))
                                        thing_2 = 'knife'
                                        keys['KnifeE'] = False
                                    else:
                                        things.append(Knife(5, 305, knife2_img))
                                        thing_3 = 'knife'
                                        keys['KnifeE'] = False
                                    material = 'knife'
                                else:
                                    sys_switch = 'things_full'

            if material == 'knife' or material == 'cut':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_j:
                        if material == 'knife':
                            material = 'cut'
                            cut_currentTime = time.time()
                            cut_interval = 0.05
                        elif material == 'cut':
                            material = 'knife'

            if level != 3:
                if door_tipPaint:
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_e:
                            answer = easygui.buttonbox('是否出去?(出去了便不可回来)', '询问', ['是的', '不是的'])
                            if answer == '是的':
                                level = 3
                                player.x = 511
                                player.y = 600
                                direction = 'UP'
                                sys_switch = 'zombie so many'

            if sys_switch != 0:
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    sys_switch = 0
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    sys_switch = 0

            if plus_bullet_tip:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_e:
                        if material == 'ak':
                            if ak.bullet_num == 50:
                                sys_switch = 'bullet_full'
                            else:
                                achievement_pip[4] = 4
                                sys_switch = 'loading bullet'
                                ak_bullet_interval = 50 - ak.bullet_num
                                ak_bullet_interval /= 100
                                ak_bullet_loadSwitch = True
                                ak_bullet_currentTime = time.time()
                        if material == 'pistol':
                            if pistol.bullet_num == 7:
                                sys_switch = 'bullet_full'
                            else:
                                achievement_pip[4] = 4
                                sys_switch = 'loading bullet'
                                pistol_bullet_interval = 7 - pistol.bullet_num
                                pistol_bullet_interval /= 100
                                pistol_bullet_loadSwitch = True
                                pistol_bullet_currentTime = time.time()
                        if material == 'machine_gun':
                            if machine_gun.bullet_num == 200:
                                sys_switch = 'bullet_full'
                            else:
                                achievement_pip[4] = 4
                                sys_switch = 'loading bullet'
                                machine_gun_bullet_interval = 200 - machine_gun.bullet_num
                                machine_gun_bullet_interval /= 100
                                machine_gun_bullet_loadSwitch = True
                                machine_gun_bullet_currentTime = time.time()

            if level != 3:
                if paint_takeMachineGun:
                    if keys['MachineGun']:
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_e:
                                achievement_pip[1] = 1
                                if len(things) < 3:
                                    paint_takeMachineGun = False
                                    paint_MachineGun = False
                                    if len(things) == 0:
                                        things.append(Machine_gun(5, 154, machine_gun2))
                                        thing_1 = 'machine_gun'
                                        keys['MachineGun'] = False
                                    elif len(things) == 1:
                                        things.append(Machine_gun(5, 229, machine_gun2))
                                        thing_2 = 'machine_gun'
                                        keys['MachineGun'] = False
                                    else:
                                        things.append(Machine_gun(5, 305, machine_gun2))
                                        thing_3 = 'machine_gun'
                                        keys['MachineGun'] = False
                                    material = 'machine_gun'
                                else:
                                    sys_switch = 'things_full'

            if return_life_tip:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_e:
                        if player.life == 100:
                            sys_switch = 'playerLife_full'
                        else:
                            achievement_pip[5] = 5
                            sys_switch = 'return life'
                            returnLife_currentTime = time.time()
                            returnLife_interval = 100 - player.life
                            returnLife_interval /= 100
                            returnLife_switch = True
        if state == 'END':
            if gameOver:
                if event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
                    easygui.msgbox('正在跳转到游戏开始的界面,请等待...')
                    pygame.time.delay(2000)
                    initGame()


# 创建comPaint方法
def comPaint():
    global zombie_num, zombie_img, direction, wave, sys_switch, answer, hit_wall, gameOver
    if state == 'START':
        canvas.blit(white, (-10, -10))
        canvas.blit(bg, (0, 0))
        if level == 'open Achievement column':
            canvas.blit(achievement_column, (257, 112))
            with open('escape-master/info.txt', 'r', encoding='utf-8') as f:
                data = f.readlines()
            for c in data:
                c = c.strip()
                newdata = json.loads(c)
                if newdata['name'] == username:
                    if 0 in newdata['achievement']:
                        canvas.blit(pick_up_pistol_score, (achievement_pos[0][0], achievement_pos[0][1]))
                    else:
                        canvas.blit(pick_up_pistol_score_not, (achievement_pos[0][0], achievement_pos[0][1]))
                    if 1 in newdata['achievement']:
                        canvas.blit(pick_up_machineGun_score, (achievement_pos[1][0], achievement_pos[1][1]))
                    else:
                        canvas.blit(pick_up_machineGun_score_not, (achievement_pos[1][0], achievement_pos[1][1]))
                    if 2 in newdata['achievement']:
                        canvas.blit(pick_up_knife_score, (achievement_pos[2][0], achievement_pos[2][1]))
                    else:
                        canvas.blit(pick_up_knife_score_not, (achievement_pos[2][0], achievement_pos[2][1]))
                    if 3 in newdata['achievement']:
                        canvas.blit(hit_zombies50_score, (achievement_pos[3][0], achievement_pos[3][1]))
                    else:
                        canvas.blit(hit_zombies50_score_not, (achievement_pos[3][0], achievement_pos[3][1]))
                    if 4 in newdata['achievement']:
                        canvas.blit(return_bullet_score, (achievement_pos[4][0], achievement_pos[4][1]))
                    else:
                        canvas.blit(return_bullet_score_not, (achievement_pos[4][0], achievement_pos[4][1]))
                    if 5 in newdata['achievement']:
                        canvas.blit(return_life_score, (achievement_pos[5][0], achievement_pos[5][1]))
                    else:
                        canvas.blit(return_life_score_not, (achievement_pos[5][0], achievement_pos[5][1]))
                    if 6 in newdata['achievement']:
                        canvas.blit(hit_wall10_score, (achievement_pos[6][0], achievement_pos[6][1]))
                    else:
                        canvas.blit(hit_wall10_score_not, (achievement_pos[6][0], achievement_pos[6][1]))
                    if 7 in newdata['achievement']:
                        canvas.blit(hit_zombies_life10_score, (achievement_pos[7][0], achievement_pos[7][1]))
                    else:
                        canvas.blit(hit_zombies_life10_score_not, (achievement_pos[7][0], achievement_pos[7][1]))
                    if 8 in newdata['achievement']:
                        canvas.blit(hit_zombies_strong_score, (achievement_pos[8][0], achievement_pos[8][1]))
                    else:
                        canvas.blit(hit_zombies_strong_score_not, (achievement_pos[8][0], achievement_pos[8][1]))

        if level == 'open ranking list':
            canvas.blit(ranking_list, (53, 29))
            with open('escape-master/info.txt', 'r', encoding='utf-8') as f:
                ranking_data = f.readlines()
            i = 0
            for c in ranking_data:
                c = c.strip()
                new_data = json.loads(c)
                if len(ranking_data) == 1:
                    fillText('名字: ' + str(new_data['name']) + '  击杀数: ' + str(new_data['kill']) + '  波数: ' + str(new_data['wave']) + '  时间: ' + str(new_data['time']), (218, 149), (0, 0, 0))
                    break
                else:
                    # for i in range(0, len(ranking_data)):
                    fillText('  名字: ' + str(new_data['name']) + '  击杀数: ' + str(new_data['kill']) + '  波数: ' + str(new_data['wave']) + '  时间: ' + str(new_data['time']), (218, ranking_list_y[0]+i*80), (0, 0, 0))
                    i+=1
                if i==7:
                    break
        if level == 'open operation guide':
            canvas.blit(operation_guide, (21, 17))
    if state == 'RUNNING':
        if level == 2:
            canvas.blit(white, (-10, -10))
            canvas.blit(running_bg, (0, 0))
            canvas.blit(carpet, (510, 365))
            door.paint()
            if knife_switch:
                knife.paint()
            if ak_switch:
                ak.paint()
            if pistol_switch:
                pistol.paint()
            if paint_MachineGun:
                machine_gun.paint()
            canvas.blit(box, (0, 150))
            for i in things:
                i.paint()
            player.paint()
            if ak_tipPaint:
                if ak_switch:
                    canvas.blit(ak_tip, (30, 30))
            elif pistol_tipPaint:
                if pistol_switch:
                    canvas.blit(pistol_tip, (30, 30))
            elif knife_tipPaint:
                if knife_switch:
                    canvas.blit(knife_tip, (30, 30))
            elif paint_takeMachineGun:
                if paint_MachineGun:
                    canvas.blit(takeMachine_gun_tip, (30, 30))
            elif door_tipPaint:
                canvas.blit(open_door_tip, (30, 30))

        if level == 3:
            canvas.blit(white, (-10, -10))
            canvas.blit(level3_bg, (0, 0))
            ammunition_depot.paint()
            treatment_box.paint()
            player.paint()
            fillText(f'第{wave}波', (974, 46), (255, 255, 255))
            canvas.blit(box, (0, 150))
            for i in things:
                i.paint()

            if zombie_num != 0:
                for i in range(zombie_num):
                    zombies.append(Zombie(random.randint(32, 726 - 150), random.randint(79, 1147 - 150), zombie_img))
                    zombie_num -= 1

            if len(zombies) != 0:
                for i in zombies:
                    i.paint()
                    i.move(random.randint(1, 10))

        if len(bullets) != 0:
            if first == 'UP':
                for i in bullets:
                    i.speed = 80
                    i.paint()
                    i.move()
            elif first == 'DOWN':
                for i in bullets:
                    i.speed = -80
                    i.paint()
                    i.move()
            elif first == 'LEFT':
                for i in bullets:
                    i.paint()
                    i.x -= 80
            elif first == 'RIGHT':
                for i in bullets:
                    i.paint()
                    i.x += 80

        if plus_bullet_tip:
            canvas.blit(load_bullet_tip, (100, 68))

        if return_life_tip:
            canvas.blit(return_life_icon, (100, 68))

        if sys_switch == 'hit wall':
            if level != 3:
                sysFont("哎呦, 好痛")
                hit_wall += 1
        if sys_switch == 'zombie so many':
            if thing_1 == 'ak' or thing_2 == 'ak' or thing_3 == 'ak':
                sysFont('这怎么那么多僵尸?快拿出武器按J打它们')
            elif thing_1 == 'knife' or thing_2 == 'knife' or thing_3 == 'knife':
                sysFont('这怎么那么多僵尸?快拿出武器按J打它们')
            elif thing_1 == 'pistol' or thing_2 == 'pistol' or thing_3 == 'pistol':
                sysFont('这怎么那么多僵尸?快拿出武器按J打它们')
            elif len(things) == 0:
                sysFont('这怎么那么多僵尸?你竟然没武器,哎!(摇头)')
        if sys_switch == 'bullet_full':
            if material == 'ak':
                sysFont('你的ak子弹满了')
            elif material == 'pistol':
                sysFont('你的手枪子弹满了')
            elif material == 'machine_gun':
                sysFont('你的机枪子弹满了')
        if sys_switch == 'loading bullet':
            sysFont('正在加子弹')
            if material == 'ak':
                if thing_1 == 'ak':
                    canvas.blit(bullet_load_icon, (43, 193))
                elif thing_2 == 'ak':
                    canvas.blit(bullet_load_icon, (43, 267))
                else:
                    canvas.blit(bullet_load_icon, (43, 341))
            elif material == 'pistol':
                if thing_1 == 'pistol':
                    canvas.blit(bullet_load_icon, (43, 193))
                elif thing_2 == 'pistol':
                    canvas.blit(bullet_load_icon, (43, 267))
                else:
                    canvas.blit(bullet_load_icon, (43, 341))
        if sys_switch == 'playerLife_full':
            sysFont('满血状态!')
        if sys_switch == 'return life':
            sysFont('正在加血')
        if sys_switch == 'things_full':
            sysFont('你的物品栏满了')
        if sys_switch == 'go_neverLevel':
            sysFont('看地上出现了一个箭头,我们可以去楼顶了!')
        if sys_switch == 'see sun':
            sysFont('啊!终于看见太阳了,感觉充满了力量!')

    if state == 'END':
        canvas.blit(game_over_img, (0, 0))
        fillText(f'玩家 : {username}', (452, 192), (0, 0, 0))
        fillText(f'你打了{wave}波僵尸', (452, 250), (0, 0, 0))
        fillText(f'你击杀了{kill_num}只僵尸', (452, 300), (0, 0, 0))
        fillText(f'时间: {time_now}', (452, 350), (0, 0, 0))
        gameOver = True


# 创建comMove方法
def comMove():
    if state == 'RUNNING':
        if level == 3:
            if keys['UP']:
                player.y -= 15
            elif keys['DOWN']:
                player.y += 15
            elif keys['LEFT']:
                player.x -= 15
            elif keys['RIGHT']:
                player.x += 15
        else:
            if keys['UP']:
                player.y -= 8
            elif keys['DOWN']:
                player.y += 8
            elif keys['LEFT']:
                player.x -= 8
            elif keys['RIGHT']:
                player.x += 8


# 创建comOut方法
def comOut():
    global material, sys_switch
    if state == 'RUNNING':
        if level == 2:
            if player.y <= 228:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.y += 20
            elif player.y + player.height >= 708:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.y -= 20
            elif player.x <= 193:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.x += 20
            elif player.x + player.width >= 1089:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.x -= 20
        elif level == 3:
            if player.y <= 0:
                if material == 'cut':
                    material = 'knife'
                player.y += 20
            elif player.y + player.height >= 758:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.y -= 20
            elif player.x <= 63:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.x += 20
            elif player.x + player.width >= 1171:
                sys_switch = 'hit wall'
                if material == 'cut':
                    material = 'knife'
                player.x -= 20
            if len(zombies) != 0:
                for i in zombies:
                    if i.y <= 0:
                        i.y += 5
                    elif i.y + i.height >= 758:
                        i.y -= 5
                    elif i.x <= 63:
                        i.x += 5
                    elif i.x + i.width >= 1171:
                        i.x -= 5

        if len(bullets) != 0:
            for i in bullets:
                if i.y <= 0:
                    bullets.remove(i)
                elif i.y + i.height >= 770:
                    bullets.remove(i)
                elif i.x <= 0:
                    bullets.remove(i)
                elif i.x + i.width >= 1232:
                    bullets.remove(i)


# 创建comHit方法
def comHit():
    global ak_tipPaint, pistol_tipPaint, knife_tipPaint, door_tipPaint, material, zombie_hit, state, plus_bullet_tip, return_life_tip, paint_takeMachineGun, kill_num
    if state == 'RUNNING':
        if player.hit(ak):
            pistol_tipPaint = False
            knife_tipPaint = False
            door_tipPaint = False
            paint_takeMachineGun = False
            ak_tipPaint = True
        else:
            ak_tipPaint = False
        if pistol.hit(player):
            ak_tipPaint = False
            knife_tipPaint = False
            door_tipPaint = False
            paint_takeMachineGun = False
            pistol_tipPaint = True
        else:
            pistol_tipPaint = False
        if player.hit(knife):
            ak_tipPaint = False
            pistol_tipPaint = False
            door_tipPaint = False
            knife_tipPaint = True
            paint_takeMachineGun = False
        else:
            knife_tipPaint = False
        if player.hit(door):
            ak_tipPaint = False
            pistol_tipPaint = False
            knife_tipPaint = False
            door_tipPaint = True
            paint_takeMachineGun = False
        else:
            door_tipPaint = False

        if player.hit(machine_gun):
            ak_tipPaint = False
            pistol_tipPaint = False
            knife_tipPaint = False
            door_tipPaint = False
            paint_takeMachineGun = True
        else:
            paint_takeMachineGun = False

        if level == 3 or level == 'hit boss':
            # 判断子弹是否碰到僵尸
            for z in zombies:
                if z.life <= 0:
                    kill_num += 1
                    zombies.remove(z)
                for i in bullets:
                    if i.hit(z):
                        bullets.remove(i)
                        if material == 'ak':
                            z.life -= 30
                        elif material == 'pistol':
                            z.life -= 20
                        elif material == 'machine_gun':
                            z.life -= 35

            # 判断僵尸是否碰到人类
            for z in zombies:
                if z.hit(player):
                    if material != 'cut':
                        zombie_hit = True
                        player.life -= 1
                        if player.life == 0:
                            file()
                            state = 'END'

                        if player.life <= 10:
                            achievement_pip[7] = 7
                    else:
                        z.life -= 10
                    # 判断僵尸血量是否为零
                    if z.life <= 0:
                        kill_num += 1
                        zombies.remove(z)
                else:
                    zombie_hit = False

            # 判断玩家是否碰到治疗箱
            if treatment_box.hit(player):
                plus_bullet_tip = False
                return_life_tip = True
            else:
                return_life_tip = False

            # 判断玩家是否碰到弹药库
            if ammunition_depot.hit(player):
                if material == 'pistol' or material == 'ak' or material == 'machine_gun':
                    plus_bullet_tip = True
                    return_life_tip = False
            else:
                plus_bullet_tip = False


# 定义判断方法
def judge():
    global zombie_num, wave, zombie_num_init
    if level == 3:
        if len(zombies) == 0:
            zombies.clear()
            zombie_num_init += 10
            zombie_num = zombie_num_init
            wave += 1
        if kill_num >= 50:
            achievement_pip[3] = 3
        if kill_num >= 100:
            achievement_pip[8] = 8
        if hit_wall >= 10:
            achievement_pip[6] = 6


while True:
    if state == 'RUNNING':
        # 刀的技能时间
        if material == 'cut':
            keys['DOWN'] = False
            keys['UP'] = False
            keys['LEFT'] = False
            keys['RIGHT'] = False
            cut_lastTime = time.time()
            if cut_lastTime - cut_currentTime >= cut_interval:
                material = 'knife'

        # ak加子弹的过程
        if ak_bullet_loadSwitch:
            keys['DOWN'] = False
            keys['UP'] = False
            keys['LEFT'] = False
            keys['RIGHT'] = False
            ak_bullet_lastTime = time.time()
            if ak_bullet_lastTime - ak_bullet_currentTime >= ak_bullet_interval:
                sys_switch = 'bullet_full'
                ak.bullet_num = 50
                ak_bullet_loadSwitch = False

        # 手枪加子弹的过程
        if pistol_bullet_loadSwitch:
            keys['DOWN'] = False
            keys['UP'] = False
            keys['LEFT'] = False
            keys['RIGHT'] = False
            pistol_bullet_lastTime = time.time()
            if pistol_bullet_lastTime - pistol_bullet_currentTime >= pistol_bullet_interval:
                sys_switch = 'bullet_full'
                pistol.bullet_num = 7
                pistol_bullet_loadSwitch = False

        # 人物回血的过程
        if returnLife_switch:
            returnLife_lastTime = time.time()
            keys['DOWN'] = False
            keys['UP'] = False
            keys['LEFT'] = False
            keys['RIGHT'] = False
            if returnLife_lastTime - returnLife_currentTime >= returnLife_interval:
                sys_switch = 'playerLife_full'
                player.life = 100
                returnLife_switch = False

        # 机枪加子弹
        if machine_gun_bullet_loadSwitch:
            keys['DOWN'] = False
            keys['UP'] = False
            keys['LEFT'] = False
            keys['RIGHT'] = False
            machine_gun_bullet_lastTime = time.time()
            if machine_gun_bullet_lastTime - machine_gun_bullet_currentTime >= machine_gun_bullet_interval:
                sys_switch = 'bullet_full'
                machine_gun.bullet_num = 200
                machine_gun_bullet_loadSwitch = False

    comPaint()
    comMove()
    comOut()
    comHit()
    judge()
    handleEvent()
    pygame.display.update()
